class Access extends DevAccess{
    Employee emp
    Area area
    Container container

    //static belongsTo = [Employee, Area, Container]

    static constraints = {
        emp(blank:false, unique:true)
        container(blank:false)
        area(blank:false)
    }

    public Boolean isBudgetLocked(){
        return container.budgetLocked;
    }

    public Boolean checkTab(String tab){
        if(hasAdminAuth()){
            return container.hasTab(tab)
        }
        else if(hasManagerAuth() || hasDirectorAuth()){
            return area.hasTab(tab)
        }
        else{
            return false
        }
    }
    /*---------------------------------------------------------------------
     *----Filter the list for controllers, deppindign on access rights-----
     *---------------------------------------------------------------------
     */
    public List getList(String controller){
        switch(controller){
    /*---------------------------------------------------------------------
     *----          Return a list with the right access               -----
     *---------------------------------------------------------------------
     */
            case "access" :
            ArrayList list = new ArrayList()

                if(hasAdminAuth()){
                    def areas = container.areas
                    def temp
                    def access
                    for(Area a : areas){
                        temp = Employee.findAllByHiredBy(a)
                        for(Employee e in temp){
                            access = Access.findAllByEmp(e)
                            for(Access ac in access){
                                    if(ac){
                                        list.add(ac)
                                    }
                            }
                        }
                    }
                }
                return list
            break
    /*---------------------------------------------------------------------
     *----          Return a list with the right projects             -----
     *---------------------------------------------------------------------
     */
            case "project" :
                if(hasAdminAuth()){
                    ArrayList list = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
//                        println("area"+a)
                        def temp
                        if(DBConnection.isConnectionStarted()){
                            temp = DBConnection.findProjectByArea(a)
                        }
                        else{
                            temp = Project.findAllByArea(a)
                        }
                        for( Project p : temp){
                            if(p){
                                list.add(p)
                            }
                        }
                    }
                    return list
                }
                else if(hasManagerAuth() || hasDirectorAuth()){
                    def temp
                    if(DBConnection.isConnectionStarted()){
                        temp = DBConnection.findProjectByArea(this.area)
                    }
                    else{
                        temp = Project.findAllByArea(this.area)
                    }
                    return temp
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
    /*---------------------------------------------------------------------
     *----          Return a list with the right course               -----
     *---------------------------------------------------------------------
     */
            case "course" :
                if(hasAdminAuth()){
                    ArrayList  list = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp = Course.findAllByArea(a)
                        for( Course c : temp){
                            if(c){
                                list.add(c)
                            }
                        }
                    }
                    return list
                }
                else if(hasManagerAuth() || hasDirectorAuth()){
                    return Course.findAllByArea(this.area)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            /*---------------------------------------------------------------------
             *----          Return a list with the right area                 -----
             *---------------------------------------------------------------------
             */
            case "area" :
                if(hasAdminAuth()){
                    return Area.findAllByContainer(this.container)
                }
                else if(hasManagerAuth() || hasDirectorAuth()){
                    return Area.findAllById(this.area.id)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            /*---------------------------------------------------------------------
             *----          Return a list with the right employees            -----
             *---------------------------------------------------------------------
             */
            case "employee" :
                if(hasAdminAuth()){
                    ArrayList emp = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp
                        temp = DBConnection.getEmployeeListByArea(a)
                        for(Employee e : temp){
                            if(!emp.contains(e)){
                                emp.add(e)
                            }
                        }
                    }
                    return emp
                }
                else if(hasManagerAuth() || hasDirectorAuth()){
                    return DBConnection.getEmployeeListByArea(this.area)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            /*---------------------------------------------------------------------
             *----          Return a list with the right employees for a course            -----
             *---------------------------------------------------------------------
             */
            case "employeecourse" :
                if(hasAdminAuth()){
                    ArrayList emp = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp
                        temp = Employee.findAllByHiredByAndMode(a,0)
                        for(Employee e : temp){
                            if(!emp.contains(e)){
                            emp.add(e)
                            }
                        }
                    }
                    return emp
                }
                else if(hasManagerAuth() || hasDirectorAuth()){
                    return Employee.findAllByHiredByAndMode(this.area,0)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            case "employeeInContainer" :
                if(hasAdminAuth() || hasManagerAuth() || hasDirectorAuth()){
                    ArrayList emp = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp
                        temp = DBConnection.getEmployeeListByArea(a)
                        for(Employee e : temp){
                            emp.add(e)
                        }
                    }
                    return emp
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            default:
                List list = new ArrayList()
                return  list
            break;
        }
    }

    public Boolean checkAccess(String mode, String controller, String action, Long id){
       // if(controller.equalsIgnoreCase("area")){
            switch(action){
                case "show" :
                    def area = Area.get(id)
                    if(hasAdminAuth()){
                        return this.container.equals(Container.get(area.container.id))
                    }
                    if(hasManagerAuth() || hasDirectorAuth()){
                        def a = Area.get(id)
                        return (this.area.id.equals(a.id) && this.container.id.equals(a.container.id))
                    }
                    return false
                break
                case "create" :
                    if(hasAdminAuth()){
                        return true
                    }
                    else if(hasManagerAuth()){
                        if(!controller.equalsIgnoreCase("area")){
                            return (mode.equalsIgnoreCase("sandbox") || mode == null)
                        }
                    }
                    else if(hasDirectorAuth()){
                        if(controller.equalsIgnoreCase("course")){
                                return true
                        }
                        else if(!controller.equalsIgnoreCase("area")){
                            return(mode.equalsIgnoreCase("sandbox") || mode == null)
                        }
                        return false
                    }
                    return false
                break
                case "edit" :
                    if(hasAdminAuth()){
                        def area = Area.get(id)
                        return area.container.id == this.container.id
                    }
                    else if(hasManagerAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            def area = Area.get(id)
                            return (this.area.id.equals(area.id) && this.container.id.equals(area.container.id))
                        }
                        else{
                            return false
                        }
                    }
                    else if(hasDirectorAuth()){
                        if(controller.equals("course")){
                            def area = Area.get(id)
                            return (this.area.id.equals(area.id) && this.container.id.equals(area.container.id))
                        }
                        else if(!controller.equalsIgnoreCase("area")){
                            if(mode.equalsIgnoreCase("sandbox") || mode == null){
                                def area = Area.get(id)
                                return (this.area.id.equals(area.id) && this.container.id.equals(area.container.id))
                            }
                        }
                        return false
                    }
                    return false
                break
                case "delete" :
                    if(hasAdminAuth()){
                        return this.container.equals(Container.get(id))
                    }
                    else if(hasManagerAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            println("In sandbox mode")
                            if(controller.equalsIgnoreCase("employee") || controller.equalsIgnoreCase("project")){
                                println("Is in controller employee or project")
                                return this.container.equals(Container.get(id))
                            }
                        }
                        return false
                    }
                    else if(hasDirectorAuth()){
                        if(controller.equals("course")){
                            return this.container.equals(Container.get(id))
                        }
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            if(controller.equalsIgnoreCase("employee") || controller.equalsIgnoreCase("project")){
                                return this.container.equals(Container.get(id))
                            }
                        }
                        return false
                    }
                    return false
                break
                case "save" :
                    println("Save " + controller)
                    if(hasAdminAuth()){
                        println("Admin")
                        return this.container.id == id
                    }
                    else if(hasManagerAuth()){
                        println("Manger")
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            if(controller.equalsIgnoreCase("employee") || controller.equalsIgnoreCase("project")){
                                return this.container.id.equals(id)
                            }
                        }
                    }
                    else if(hasDirectorAuth()){
                        if(controller.equalsIgnoreCase("course")){
                            return this.container.id.equals(id)
                        }
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            if(controller.equalsIgnoreCase("employee") || controller.equalsIgnoreCase("project")){
                                return this.container.equals(Container.get(id))
                            }
                        }
                    }
                    else{
                        return false
                    }
                break
                case "addEmployee" :
                    if(hasAdminAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            def areas = container.areas
                            for(Area a : areas){
                                if(a.id.equals(id)){
                                    return true
                                }
                            }
                            return false
                        }
                        else{
                            if(this.container.budgetLocked){
                                return false;
                            }
                            else{
                                def areas = container.areas
                                for(Area a : areas){
                                    if(a.id.equals(id)){
                                        return true
                                    }
                                }
                                return false
                            }
                        }
                    }
                    if(hasManagerAuth() || hasDirectorAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            return this.area.id.equals(id)
                        }
                        else{
                            return (!this.container.budgetLocked && this.area.id.equals(id))
                        }
                    }
                break
                case "deleteEmployee" :
                    if(hasAdminAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            def areas = container.areas
                            for(Area a : areas){
                                if(a.id.equals(id)){
                                    return true
                                }
                            }
                            return false
                        }
                        else{
                            if(this.container.budgetLocked){
                                return false;
                            }
                            else{
                                def areas = container.areas
                                for(Area a : areas){
                                    if(a.id.equals(id)){
                                        return true
                                    }
                                }
                                return false
                            }
                        }
                        
                    }
                    if(hasManagerAuth() || hasDirectorAuth()){
                        if(mode.equalsIgnoreCase("sandbox") || mode == null){
                            return this.area.id.equals(id)
                        }
                        else{
                            return (!this.container.budgetLocked && this.area.id.equals(id))
                        }
                    }
                break
                default : return false
                }
            }
           // else{
           //     return false
           // }
        //}
    String toString(){"${this.emp}, (${this.role})"}
    //String toString(){"${this.role}"}
}
